package edu.sysu.cshare.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name = "T_USER")
public class User implements Serializable {

	private String id;
	private String descrip;
	private String lastIP;
	private String name;
	private String nickname;
	private String password;
	private int role;
	private Clazz clazz;
	private List<Comment> comments;
	private List<Course> courses;
	private List<Group> groups;
	private List<Message> messages;
	private List<SubmitInfo> submitInfos;

	@Id
	@Column(length = 20)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(length = 255)
	public String getDescrip() {
		return descrip;
	}

	public void setDescrip(String descrip) {
		this.descrip = descrip;
	}

	@Column(length = 15)
	public String getLastIP() {
		return lastIP;
	}

	public void setLastIP(String lastIP) {
		this.lastIP = lastIP;
	}

	@Column(length = 50)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(length = 30)
	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	@Column(length = 50)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getRole() {
		return role;
	}

	public void setRole(int role) {
		this.role = role;
	}

	@ManyToOne
	public Clazz getClazz() {
		return clazz;
	}

	public void setClazz(Clazz clazz) {
		this.clazz = clazz;
	}

	@OneToMany(mappedBy = "commenter", fetch = FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	public List<Comment> getComments() {
		return comments;
	}

	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	@ManyToMany(targetEntity = Course.class, fetch = FetchType.EAGER)
	@JoinTable(name = "TR_USER_COURSE", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "course_id"))
	@Fetch(FetchMode.SUBSELECT)
	public List<Course> getCourses() {
		return courses;
	}

	public void setCourses(List<Course> courses) {
		this.courses = courses;
	}

	@ManyToMany(mappedBy = "members", targetEntity = Group.class, fetch = FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	public List<Group> getGroups() {
		return groups;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	@OneToMany(mappedBy = "publisher", fetch = FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	public List<Message> getMessages() {
		return messages;
	}

	public void setMessages(List<Message> messages) {
		this.messages = messages;
	}

	@OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	public List<SubmitInfo> getSubmitInfos() {
		return submitInfos;
	}

	public void setSubmitInfos(List<SubmitInfo> submitInfos) {
		this.submitInfos = submitInfos;
	}

}
